package com.wn.web;

import com.wn.common.annotation.RequireRoles;
import com.wn.common.pojo.PageParam;
import com.wn.common.pojo.Rsp;
import com.wn.common.pojo.User;
import com.wn.common.util.Const;
import com.wn.common.util.JWTUtil;
import com.wn.service.UserService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * @BelongsProject: hurricane_travel
 * @BelongsPackage: com.wn.web
 * @Author: lhq
 * @Description:
 */
@RestController
@RestControllerAdvice
@Api(value = "用户接口",description = "用户接口")
@RequestMapping("user")
public class UserController {

    private static Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * 登出
     *
     * @param
     * @return
     */
    @PostMapping("/logout")
    public Rsp logout(@RequestHeader("token") String token) {

        return Rsp.success("登出成功");
    }

    /**
     * 登录接口
     *
     * @return
     */
    @PostMapping(value = "login")
    public Rsp login(@RequestBody User user) throws Exception {

        Rsp login = userService.login(user);

           return login;
    }

    /**
     * 查询所有用户接口
     *
     * @return
     */
    @PostMapping(value = "get_user_list", produces = "application/json;charset=utf-8")
    @RequireRoles(roles = "super_admin", index = 0, type = User.class)
    public Rsp getUserList(@RequestHeader("token") String token, @RequestBody PageParam<User> pageParam) {

        return new Rsp().success(userService.getAllUsers(pageParam));
    }


    /**
     *前端自动调用显示分角色首页
     * @return
     */
    @PostMapping("getUserInfo")
    @RequireRoles(roles = {"super_admin","member","admin"},index = 0,type = User.class)
    public Rsp getUserInfo(@RequestHeader("token") String token) throws Exception {
        User user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);

        return Rsp.success(userService.selectUserByAccount(user.getUserAccount()));
    }


    /**
     * 逻辑删除指定用户
     *
     * @param
     * @param
     * @return
     */
    @PostMapping(value = "delete_user", produces = "application/json;charset=utf-8")
    @RequireRoles(roles = {"super_admin"}, index = 1, type = User.class)
    public Rsp deleteUser(@RequestBody User user, @RequestHeader("token") String token) {

        return userService.deleteUser(user);

    }
    /**
     * 管理员修改用户信息接口
     * @param record
     * @return
     */
    @PostMapping(value = "update_user", produces = "application/json;charset=utf-8")
    @RequireRoles(roles = {"super_admin","member","admin"}, index = 1, type = User.class)
    public @ResponseBody
    Rsp updateUser(@RequestBody @Valid User record, @RequestHeader("token") String token) {
        return userService.updateUser(record);

    }

    /**
     * 用户校验接口
     * @param email
     * @param account
     * @return
     */
    @GetMapping(value="check_user")
    public @ResponseBody Rsp checkUser(@RequestParam("email") String email,@RequestParam("account") String account){
        Rsp rsp = userService.checkUser(account, email);
        return rsp;
    }

    /**
     * 用户注册接口
     *
     * @param user
     * @return
     */
    @PostMapping(value = "register_user")
    public Rsp registerUser(@RequestBody @Valid User user) {
        return userService.userRegister(user);

    }

    /**
     * 登录状态下重置密码
     *
     * @param
     * @return
     */
    @PostMapping(value = "reset_password")
    @RequireRoles(roles = {"admin","super_admin","member"},index = 1,type=User.class)
    public Rsp resetPassword(@RequestBody User user,@RequestHeader("token")String token) {

        return userService.updataUserPassword(user.getUserAccount(), user.getUserPassword());
    }

    @PostMapping("update_status")
    @RequireRoles(roles = "super_admin", index = 1, type = User.class)
    public Rsp updateStatus(@RequestBody User user, @RequestHeader("token") String token) {
        return userService.updateStatus(user);
    }

    @PostMapping("sendEmail")
    public Rsp sendEmail(@RequestBody User user) {
      return userService.sendEmail(user.getUserEmail()) ;
    }

    /**
     * 解绑邮箱或手机
     */
    @PostMapping("unbundling")
    @RequireRoles(roles = {"super_admin","admin","member"},index=1,type=User.class)
    public Rsp unbundling(@RequestBody User user,@RequestHeader("token") String token){
        return userService.unbundling(user);
    }
}
